<?php
defined('IN_MAIN') or exit('Invalid Access!');

function check_sufficient_qty($prods){
/*
This function check every input product $prods (normal product / products within a jump start package) to see if available qty is sufficient
input $prods must have the format
$prods[index][refno]
$prods[index][qty]
if product is not exist, then it will skip to check that product.
if no product is provided, it wil return true
if all qty are sufficient, then it will return boolean true, otherwise it will return the product that the qty is insufficient
*/

	global $vars;
	$db=$vars["db"];

	//below to format the requested qty & available qty for each of the products in $prods into $chk_prod & $prod_avai_qty respectively
	//and will expand the products inside a jumpstart package into the $chk_prod & $prod_qty too
	for($i=0,$t=count($prods);$i<$t;$i++){
		$this_prod=@mysql_fetch_assoc(mysql_query("select id, prod_type, qty_type, qty from $db->products where refno='".$prods[$i]["refno"]."'")) or false;
		if($this_prod){
			if($this_prod["prod_type"]=='n'){
				$prod_qty_type[StripSlashes($prods[$i]["refno"])]=$this_prod["qty_type"];
				if($this_prod['qty_type'] == 'l'){//limited
					$prod_avai_qty[StripSlashes($prods[$i]["refno"])]=$this_prod["qty"];
					$chk_prod[StripSlashes($prods[$i]["refno"])]+=$prods[$i]["qty"];
				}
			}elseif($this_prod["prod_type"]=='p'){
				$t2=@mysql_num_rows($r2=mysql_query("select p.refno, p.qty_type, p.qty as avai_qty, j.qty as req_qty from $db->products p, $db->package_detail j where j.pkid='$this_prod[id]' and j.pid=p.id"));
				for($j=0;$j<$t2;$j++){
					$this_pck=mysql_fetch_assoc($r2);
					$prod_qty_type[$this_pck["refno"]]=$this_pck["qty_type"];
					$prod_avai_qty[$this_pck["refno"]]=$this_pck["avai_qty"];
					$chk_prod[$this_pck["refno"]]+=$this_pck["req_qty"]*$prods[$i]["qty"];
					$package_detail[StripSlashes($prods[$i]["refno"])][$j]=$this_pck["refno"];
				}
			}
		}
	}
	if(count($chk_prod)){
		foreach($chk_prod as $refno => $req_qty){
			if($prod_qty_type[$refno] == 'l' && $req_qty > $prod_avai_qty[$refno]){
				$package_ins=false;
				//below check to see if the current product belongs to the products of a package
				if(count($package_detail)){
					//below will check every package for the same product and every package will appear in the error msg in the package has the product
					foreach($package_detail as $pck_refno => $details){
						for($i=0,$t=count($details);$i<$t;$i++){
							if($details[$i] == $refno){
								$ins_qty.=replace_tag(__("Insufficient quantity for product with reference no. '<%refno%>' in the package with reference no. '<%pck_refno%>'."), array("<%refno%>"=>strval($refno), "<%pck_refno%>"=>strval($pck_refno)))."<br />\n";
								$i=$t;
								$package_ins=true;
							}
						}
					}
				}
				//if insufficient product is not in the package (means in the individual product)
				if(!$package_ins){
					$ins_qty.=replace_tag(__("Insufficient quantity for product with reference no. '<%refno%>'."), array("<%refno%>"=>strval($refno)))." ".__("Required:")." $req_qty ".__("Available:")." $prod_avai_qty[$refno]"."<br />\n";
				}
			}
		}
	}

	$return = $ins_qty? $ins_qty : true;
	return $return;
}
?>